<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, user-scalable=no">
  <meta name="description" content="API docs for the generate method from the UuidV6 class, for the Dart programming language.">
  <title>generate method - UuidV6 class - v6 library - Dart API</title>


  
  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,300;0,400;0,500;0,700;1,400&display=swap" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0" rel="stylesheet">
  
  <link rel="stylesheet" href="../../static-assets/github.css?v1">
  <link rel="stylesheet" href="../../static-assets/styles.css?v1">
  <link rel="icon" href="../../static-assets/favicon.png?v1">

  
</head>


<body data-base-href="../../" data-using-base-href="false" class="light-theme">

<div id="overlay-under-drawer"></div>

<header id="title">
  <span id="sidenav-left-toggle" class="material-symbols-outlined" role="button" tabindex="0">menu</span>
  <ol class="breadcrumbs gt-separated dark hidden-xs">
    <li><a href="../../index.html">uuid</a></li>
    <li><a href="../../v6/v6-library.html">v6</a></li>
    <li><a href="../../v6/UuidV6-class.html">UuidV6</a></li>
    <li class="self-crumb">generate method</li>
  </ol>
  <div class="self-name">generate</div>
  <form class="search navbar-right" role="search">
    <input type="text" id="search-box" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
  <div class="toggle" id="theme-button">
    <label for="theme">
      <input type="checkbox" id="theme" value="light-theme">
      <span id="dark-theme-button" class="material-symbols-outlined">
        brightness_4
      </span>
      <span id="light-theme-button" class="material-symbols-outlined">
        brightness_5
      </span>
    </label>
  </div>
</header>
<main>

  <div
      id="dartdoc-main-content"
      class="main-content"
      data-above-sidebar="v6&#47;UuidV6-class-sidebar.html"
      data-below-sidebar="">
      <div>
<h1><span class="kind-method">generate</span> method 
</h1></div>

    <section class="multi-line-signature">
      

<span class="returntype"><a href="https://api.dart.dev/stable/3.1.3/dart-core/String-class.html">String</a></span>
<span class="name ">generate</span>(<wbr><ol class="parameter-list"><li><span class="parameter" id="generate-param-options">{<span class="type-annotation"><a href="../../data/V6Options-class.html">V6Options</a>?</span> <span class="parameter-name">options</span>}</span></li>
</ol>)

      

    </section>
    
<section class="desc markdown">
  <p>v6() Generates a time-based version 6 UUID</p>
<p>By default it will generate a string based off current Gregorian epoch
time, and will return a string.</p>
<p>The first argument is an options map that takes various configuration
options detailed in the readme.</p>
<p><a href="https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format#section-4.3">https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format#section-4.3</a></p>
</section>


    
<section class="summary source-code" id="source">
  <h2><span>Implementation</span></h2>
  <pre class="language-dart"><code class="language-dart">String generate({V6Options? options}) {
  _init();
  var buf = Uint8List(16);

  int clockSeq = options?.clockSeq ?? V6State.clockSeq ?? 0;

  &#47;&#47; UUID timestamps are 100 nano-second units since the Gregorian epoch,
  &#47;&#47; (1582-10-15 00:00). Time is handled internally as &#39;msecs&#39; (integer
  &#47;&#47; milliseconds) and &#39;nsecs&#39; (100-nanoseconds offset from msecs) since unix
  &#47;&#47; epoch, 1970-01-01 00:00.
  int mSecs = options?.mSecs ?? (DateTime.now()).millisecondsSinceEpoch;

  &#47;&#47; Per 4.2.1.2, use count of uuid&#39;s generated during the current clock
  &#47;&#47; cycle to simulate higher resolution clock
  int nSecs = options?.nSecs ?? V6State.nSecs + 1;

  &#47;&#47; Time since last uuid creation (in msecs)
  var dt = (mSecs - V6State.mSecs) + (nSecs - V6State.nSecs) &#47; 10000;

  &#47;&#47; Per 4.2.1.2, Bump clockseq on clock regression
  if (dt &lt; 0 &amp;&amp; options?.clockSeq == null) {
    clockSeq = clockSeq + 1 &amp; 0x3fff;
  }

  &#47;&#47; Reset nsecs if clock regresses (new clockseq) or we&#39;ve moved onto a new
  &#47;&#47; time interval
  if ((dt &lt; 0 || mSecs &gt; V6State.mSecs) &amp;&amp; options?.nSecs == null) {
    nSecs = 0;
  }

  &#47;&#47; Per 4.2.1.2 Throw error if too many uuids are requested
  if (nSecs &gt;= 10000) {
    throw Exception(&#39;uuid.v6(): Can\&#39;t create more than 10M uuids&#47;sec&#39;);
  }

  V6State.mSecs = mSecs;
  V6State.nSecs = nSecs;
  V6State.clockSeq = clockSeq;

  &#47;&#47; Per 4.1.4 - Convert from unix epoch to Gregorian epoch
  mSecs += 12219292800000;

  var uuidTime = mSecs * 10000 + nSecs;

  var high32a = uuidTime &gt;&gt; 28;
  var high32b = uuidTime &lt;&lt; 4;
  var low = uuidTime &amp; 0x0fff | 0x6000;
  var clock = (clockSeq &amp; 0x3fff) | 0x8000;

  buf..buffer.asByteData().setUint32(0, high32a);
  buf..buffer.asByteData().setUint32(4, high32b);
  buf..buffer.asByteData().setUint16(6, low);
  buf..buffer.asByteData().setUint16(8, clock);

  var node =
      options?.node ?? V6State.nodeId ?? [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  buf.setAll(10, node);

  return UuidParsing.unparse(buf);
}</code></pre>
</section>


  </div> <!-- /.main-content -->

  <div id="dartdoc-sidebar-left" class="sidebar sidebar-offcanvas-left">
    <!-- The search input and breadcrumbs below are only responsively visible at low resolutions. -->
<header id="header-search-sidebar" class="hidden-l">
  <form class="search-sidebar" role="search">
    <input type="text" id="search-sidebar" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
</header>

<ol class="breadcrumbs gt-separated dark hidden-l" id="sidebar-nav">
    <li><a href="../../index.html">uuid</a></li>
    <li><a href="../../v6/v6-library.html">v6</a></li>
    <li><a href="../../v6/UuidV6-class.html">UuidV6</a></li>
    <li class="self-crumb">generate method</li>
</ol>


    <h5>UuidV6 class</h5>
  </div><!--/.sidebar-offcanvas-->

  <div id="dartdoc-sidebar-right" class="sidebar sidebar-offcanvas-right">
  </div><!--/.sidebar-offcanvas-->

</main>

<footer>
  <span class="no-break">
    uuid
      4.2.1
  </span>

  
</footer>



<script src="../../static-assets/highlight.pack.js?v1"></script>
<script src="../../static-assets/docs.dart.js"></script>



</body>

</html>

